//
//  DetailGoodShopViewController.m
//  BTRIP
//
//  Created by johnbk on 15/11/13.
//  Copyright © 2015年 johnbk. All rights reserved.
//

#import "DetailGoodShopViewController.h"
#import "GoodShopTopView.h"
#import "DataHandle.h"
#import <SDWebImageManager.h>
#import <UIImageView+WebCache.h>
#import "DetaiGoodShopTableViewCellOfImage.h"
#import "DetaiGoodShopTableViewCellOfLabel.h"
#import "SuitableHeight.h"
#import "DetailGoodShopCommentTableViewCell.h"
#import "Comments.h"
#import "DetailGoodShopInformationTableViewCell.h"
#import "MoreCommentViewController.h"
#import "MapViewController.h"
#import "SQLDataManager.h"
#import "CollectTravelModel.h"
@interface DetailGoodShopViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) SQLDataManager *dataManager;

@end

@implementation DetailGoodShopViewController
-(void)dealloc{
    [_idOfUrl release];
	[_imgView release];
	[_tableView release];
	[_detailGoodShopModel release];
	[_goodShopTopView release];
	[_naviView release];
	[_buttonOfLike release];
	[_buttonOfReturn release];
	[_buttonOfShare release];
	[super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _dataManager = [SQLDataManager shareSQLDataManager];
    
    if (self.goodShopModel.id != nil) {
        self.idOfUrl = self.goodShopModel.id;
    }
    
	
	[self createTableView];
	[self interperateData];
	[self createNaviView];
	
}
- (void)interperateData{
	[DataHandle contectWithURL:[NSString stringWithFormat:@"http://api.yizhong.cccwei.com/api.php?srv=2013&cid=512665&city_id=1&uid=0&tms=20151111171421&sig=af14bd9f99c6c4c1&wssig=d531d31025d1e5ff&os_type=1&version=8&shop_id=%@", _idOfUrl] andParameters:nil andViewController:self andBlock:^(id response) {
		if (response != nil) {
			
			NSDictionary *dictionary = response;
			self.detailGoodShopModel = [[DetailGoodShopModel alloc]init];
			[self.detailGoodShopModel setValuesForKeysWithDictionary:[dictionary objectForKey:@"data"]];
			//NSLog(@"🍌%@", [dictionary objectForKey:@"data"]);
			self.goodShopTopView.detailGoodShopModel = self.detailGoodShopModel;
			[self.imgView sd_setImageWithURL:[NSURL URLWithString:self.detailGoodShopModel.url] placeholderImage:[UIImage imageNamed:@"background"]];
            
#warning 收藏按钮状态判断
            [_dataManager open];
            NSArray *array = [[SQLDataManager shareSQLDataManager] searchTitleStr];
            if ([array containsObject:self.detailGoodShopModel.name]) {
                self.buttonOfLike.selected = YES;
            }
            
			[self.tableView reloadData];
			
		}
		
	}];

	
}
- (void)createTableView{
	self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, -20, self.view.frame.size.width, self.view.frame.size.height + 20) style:UITableViewStyleGrouped];
	//self.tableView.backgroundColor = [UIColor greenColor];
	[self.view addSubview:self.tableView];
	[self.tableView release];
	self.tableView.delegate = self;
	self.tableView.dataSource = self;
	self.goodShopTopView = [[GoodShopTopView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 380)];

	self.tableView.tableHeaderView = self.goodShopTopView;
	self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
	self.tableView.bounces = NO;
	
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
	return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	if (indexPath.section == 0) {
		Story *story = self.detailGoodShopModel.story[indexPath.row];
		if (story.type.integerValue == 1) {
			DetaiGoodShopTableViewCellOfLabel *cell = [DetaiGoodShopTableViewCellOfLabel tableView:tableView];
			cell.contentOnLabel = story.text;
			//cell.backgroundColor = [UIColor redColor];
			return cell;
		}
		else{
			DetaiGoodShopTableViewCellOfImage *cell = [DetaiGoodShopTableViewCellOfImage tableView:tableView];
			[cell.imgView sd_setImageWithURL:[NSURL URLWithString:story.url] placeholderImage:[UIImage imageNamed:@"background"]];
			
			//cell.backgroundColor = [UIColor yellowColor];
			return cell;
		}
	}
	else if (indexPath.section == 1){
		if (self.detailGoodShopModel.comments.count !=0) {
			if (self.detailGoodShopModel.comments.count == 1) {
				if (indexPath.row == self.detailGoodShopModel.comments.count) {
					static NSString *cellIdentifer = @"更多评论";
					UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
					if (!cell) {
						cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifer];
						
					}
					cell.textLabel.text = @"更多评论 >>";
					cell.textLabel.font = [UIFont systemFontOfSize:12];
					cell.textLabel.textAlignment = NSTextAlignmentCenter;
					return cell;
				}else{
					Comments *comments = self.detailGoodShopModel.comments[indexPath.row];
					DetailGoodShopCommentTableViewCell *cell = [DetailGoodShopCommentTableViewCell tableView:tableView];
					cell.comments = comments;
					return cell;
				}

			}else{
				if (indexPath.row == 2) {
					static NSString *cellIdentifer = @"更多评论";
					UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
					if (!cell) {
						cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifer];
						
					}
					cell.textLabel.text = @"更多评论 >>";
					cell.textLabel.font = [UIFont systemFontOfSize:12];
					cell.textLabel.textAlignment = NSTextAlignmentCenter;
					return cell;
				}else{
					Comments *comments = self.detailGoodShopModel.comments[indexPath.row];
					DetailGoodShopCommentTableViewCell *cell = [DetailGoodShopCommentTableViewCell tableView:tableView];
					cell.comments = comments;
					return cell;
				}

			}
			
		}else{
			if (indexPath.row == 1) {
				static NSString *cellIdentifer = @"更多评论";
				UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
				if (!cell) {
					cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifer];
					
				}
				cell.textLabel.text = @"更多评论 >>";
				cell.textLabel.font = [UIFont systemFontOfSize:12];
				cell.textLabel.textAlignment = NSTextAlignmentCenter;
				return cell;
			}
			else{
				UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"dainaec"];
				cell.textLabel.text = @"精致生活, 等你来体验和评论!";
				cell.textLabel.textColor = [UIColor darkGrayColor];
				cell.textLabel.font = [UIFont systemFontOfSize:11];
				cell.textLabel.textAlignment = NSTextAlignmentCenter;
				return cell;
			}
		}
		
	}
	else{
		DetailGoodShopInformationTableViewCell *cell = [DetailGoodShopInformationTableViewCell tableView:tableView];
		[cell.buttonOfLocation addTarget:self action:@selector(toMap:) forControlEvents:UIControlEventTouchUpInside];
		cell.detailGoodShopModel = self.detailGoodShopModel;
		return cell;
	}
	
	
}
- (void)toMap: (UIButton *)button{
	MapViewController *mapViewController = [[MapViewController alloc]init];
	NSArray *array = [self.detailGoodShopModel.coordinate componentsSeparatedByString:@","];
	CLLocationDegrees y = ((NSString *)array[0]).doubleValue;
	CLLocationDegrees x = ((NSString *)array[1]).doubleValue;
	mapViewController.goodShopCoordinate = CLLocationCoordinate2DMake(x, y);
	mapViewController.address = self.detailGoodShopModel.address;
	[self.navigationController pushViewController:mapViewController animated:YES];
	//[mapViewController release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
	if (section == 0) {
		return self.detailGoodShopModel.story.count;
	}
	else if (section == 1){
		if (self.detailGoodShopModel.comments.count != 0) {
			if (self.detailGoodShopModel.comments.count >= 2) {
				return 3;
			}
			else{
				return self.detailGoodShopModel.comments.count + 1;
			}
		}
		else{
			return 2;
		}
	}
	else{
		return 1;
	}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
	if (indexPath.section == 0) {
		Story *story = self.detailGoodShopModel.story[indexPath.row];
		if (story.type.integerValue == 1) {
			return [SuitableHeight suitableHeight:story.text andWidth:(self.view.frame.size.width - 40) andFont:12] + 20;
		}
		else if (story.type.integerValue == 3){
			return 190;
		}
		else{
			return 0;
		}
	}
	else if (indexPath.section == 1){
		if (self.detailGoodShopModel.comments.count != 0) {
			if (self.detailGoodShopModel.comments.count == 1) {
				if (indexPath.row == self.detailGoodShopModel.comments.count) {
					return 50;
				}
				else{
					Comments *comments = self.detailGoodShopModel.comments[indexPath.row];
					return [SuitableHeight suitableHeight:comments.content andWidth:(self.view.frame.size.width - 40) andFont:12] + 75;
					
				}
			}else{
				if (indexPath.row == 2) {
					return 50;
				}
				else{
					Comments *comments = self.detailGoodShopModel.comments[indexPath.row];
					return [SuitableHeight suitableHeight:comments.content andWidth:(self.view.frame.size.width - 40) andFont:12] + 75;
					
				}
			}
		}else{
			if (indexPath.row == 1) {
				return 50;
			}
			else{
				return 100;
				
			}
		}
		
	}
	else{
		return 180;
	}
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated{
	[self.navigationController.navigationBar setHidden:YES];
}

- (void)createNaviView{
	self.naviView = [[UIView alloc]initWithFrame:CGRectMake(0, -20, self.view.frame.size.width, 84)];
	[self.view addSubview:self.naviView];
	[self.naviView release];
	self.naviView.alpha = 0;
	self.imgView = [[UIImageView alloc]initWithFrame:self.naviView.bounds];
	[self.naviView addSubview:self.imgView];
	[self.imgView release];
	
	self.buttonOfReturn = [UIButton buttonWithType:UIButtonTypeCustom];
	self.buttonOfReturn.frame = CGRectMake(20, 35, 20, 20);
	[self.buttonOfReturn addTarget:self action:@selector(backToFront:) forControlEvents:UIControlEventTouchUpInside];
	[self.buttonOfReturn setBackgroundImage:[UIImage imageNamed:@"iconfont-fanhui-4"] forState:UIControlStateNormal];
	[self.view addSubview:self.buttonOfReturn];
	
	self.buttonOfLike = [UIButton buttonWithType:UIButtonTypeCustom];
	self.buttonOfLike.frame = CGRectMake(self.view.frame.size.width - 100, self.buttonOfReturn.frame.origin.y, self.buttonOfReturn.frame.size.width, self.buttonOfReturn.frame.size.height);
	[self.buttonOfLike setBackgroundImage:[UIImage imageNamed:@"iconfont-xin"] forState:UIControlStateNormal];
    [self.buttonOfLike setBackgroundImage:[UIImage imageNamed:@"iconfont-xin-2"] forState:UIControlStateSelected];
    [self.buttonOfLike addTarget:self action:@selector(buttonOfLikeAction:) forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview:self.buttonOfLike];
	
	self.buttonOfShare = [UIButton buttonWithType:UIButtonTypeCustom];
	self.buttonOfShare.frame = CGRectMake(self.buttonOfLike.frame.origin.x + 20 + self.buttonOfLike.frame.size.width, self.buttonOfReturn.frame.origin.y, self.buttonOfReturn.frame.size.width, self.buttonOfReturn.frame.size.height);
	[self.buttonOfShare setBackgroundImage:[UIImage imageNamed:@"iconfont-share-3"] forState:UIControlStateNormal];
	[self.view addSubview:self.buttonOfShare];
	
}
- (void)backToFront: (UIButton *)button{
	[self.navigationController popViewControllerAnimated:YES];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
	//NSLog(@" x = %f", scrollView.contentOffset.x);
	//NSLog(@" y = %f", scrollView.contentOffset.y);

	if (scrollView.contentOffset.y < 0) {
		[self.buttonOfShare setBackgroundImage:[UIImage imageNamed:@"iconfont-share-3"] forState:UIControlStateNormal];
		[self.buttonOfLike setBackgroundImage:[UIImage imageNamed:@"iconfont-xin"] forState:UIControlStateNormal];
		[self.buttonOfReturn setBackgroundImage:[UIImage imageNamed:@"iconfont-fanhui-4"] forState:UIControlStateNormal];
		self.naviView.alpha = 0;
	}
	else if (scrollView.contentOffset.y > 0 && scrollView.contentOffset.y < self.goodShopTopView.imgView.frame.size.height) {
		self.naviView.alpha = scrollView.contentOffset.y / self.goodShopTopView.imgView.frame.size.height;
		[self.buttonOfLike setBackgroundImage:[UIImage imageNamed:@"iconfont-xin-2"] forState:UIControlStateNormal];
		[self.buttonOfReturn setBackgroundImage:[UIImage imageNamed:@"iconfont-fanhui-3"] forState:UIControlStateNormal];
		[self.buttonOfShare setBackgroundImage:[UIImage imageNamed:@"iconfont-share-2"] forState:UIControlStateNormal];
	}
	else{
		//self.naviView.backgroundColor = [UIColor whiteColor];
		self.naviView.alpha = 1;
		[self.buttonOfLike setBackgroundImage:[UIImage imageNamed:@"iconfont-xin-2"] forState:UIControlStateNormal];
		[self.buttonOfReturn setBackgroundImage:[UIImage imageNamed:@"iconfont-fanhui-3"] forState:UIControlStateNormal];
		[self.buttonOfShare setBackgroundImage:[UIImage imageNamed:@"iconfont-share-2"] forState:UIControlStateNormal];
	}
	
	
	
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
	if (section == 1) {
		return @"评论精选";
	}
	else if (section == 2){
		return @"店铺信息";
	}
	else{
		return nil;
	}
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
	if (section == 1) {
		return 30;
	}
	else if (section == 2){
		return 30;
	}
	else{
		return 0;
	}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
	if (indexPath.section == 1 && self.detailGoodShopModel.comments.count >= 2) {
		MoreCommentViewController *moreComment = [[MoreCommentViewController alloc]init];
		moreComment.detailGoodModel = self.detailGoodShopModel;
		[self.navigationController pushViewController:moreComment animated:YES];
		[moreComment release];
	}
}

- (void)buttonOfLikeAction:(UIButton *)sender {
    
    sender.selected = !sender.selected;
    if (sender.selected == YES) {
        [_dataManager open];
        [_dataManager createTable];
        [_dataManager insertInfo:_detailGoodShopModel andTheID:self.goodShopModel.id];
    } else {
        [_dataManager open];
        [_dataManager deleteInfo:_detailGoodShopModel.name];
    }
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [_dataManager close];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
